home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 11 - 1995 / 11.04 Apr 95 / TreeAppƒ / Eric's C++ Libraries / Interface Classes / CPPStaticText.cp < prev    next >
Encoding:
Text File  |  1996-04-04  |  7.0 KB  |  282 lines  |  [TEXT/KAHL]

  1. /********************************************************* DEFINITION
  2.     DATE:    9/24/93
  3.     AUTHOR: Eric R. Rosé
  4.     
  5.     CLASS:  CPPStaticText
  6.     
  7.     SUPERCLASS: CPPVisualObject
  8.     
  9.         This C++ class manages a static string
  10.     
  11. ********************************************************************/
  12.  
  13. #include <CPPStaticText.h>
  14. #include <CPPWindow.h>
  15. #include <MemoryTools.h>
  16. #include <StringTools.h>
  17.  
  18. /*-----------------------------------------------------------------*/
  19. /*------------------------ PUBLIC METHODS -------------------------*/
  20. /*-----------------------------------------------------------------*/
  21.  
  22.     CPPStaticText::CPPStaticText (CPPWindow *itsWindow, Rect *itsBounds,
  23.                                    int StrID, int Font, int FSize,
  24.                                    int FJust, int FStyle,
  25.                                    RGBColor *FColor, Boolean canBeTarget,
  26.                                    Boolean active, Boolean visible) :
  27.                     CPPVisualObject (itsWindow, itsBounds, canBeTarget,
  28.                                      active, visible)
  29.     {
  30.         StringHandle    tempHandle = GetString(StrID);
  31.         
  32.         if (tempHandle)
  33.           {
  34.               this->itemText = (StringPtr)Hand2Ptr((Handle)tempHandle);
  35.               ReleaseResource((Handle)tempHandle);
  36.           }
  37.         else
  38.           this->itemText = NULL;
  39.           
  40.         MakeStatText (itsBounds, Font, FSize, FJust, FStyle, FColor);
  41.     }
  42.  
  43. /*-----------------------------------------------------------------*/
  44.  
  45.     CPPStaticText::CPPStaticText (CPPWindow *itsWindow, Rect *itsBounds,
  46.                                    StringPtr theString, int Font, 
  47.                                    int FSize, int FJust, 
  48.                                    int FStyle, RGBColor *FColor,
  49.                                    Boolean canBeTarget, Boolean active, 
  50.                                    Boolean visible) :
  51.                     CPPVisualObject (itsWindow, itsBounds, canBeTarget,
  52.                                      active, visible)
  53.     {
  54.         if (theString)
  55.           this->itemText = String2String (theString);
  56.         else
  57.           this->itemText = NULL;
  58.         MakeStatText (itsBounds, Font, FSize, FJust, FStyle, FColor);
  59.     }
  60.  
  61. /*-----------------------------------------------------------------*/
  62.  
  63.     CPPStaticText::~CPPStaticText (void)
  64.     {
  65.         NukePtr(this->itemText);
  66.     }
  67.  
  68. /*-----------------------------------------------------------------*/
  69.  
  70.     Boolean    CPPStaticText::Member (char *className)
  71.     {
  72.         if (strcmp(className, CPPStaticText::ClassName()) == 0)
  73.           return TRUE;
  74.         else
  75.           return CPPVisualObject::Member(className);
  76.     }
  77.     
  78. /*-----------------------------------------------------------------*/
  79.  
  80.     char    *CPPStaticText::ClassName (void)
  81.     {
  82.         return "CPPStaticText";
  83.     }
  84.  
  85. /*-----------------------------------------------------------------*/
  86.  
  87.     void    CPPStaticText::Draw (void)
  88.     {
  89.         RGBColor    OldColor;
  90.         
  91.         if (this->owningWindow && this->itemText && this->IsVisible())
  92.           {
  93.               // save the old values for the font, face, style & color
  94.             short OldFont = this->owningWindow->txFont;
  95.             short OldSize = this->owningWindow->txSize;
  96.             short OldFace = this->owningWindow->txFace;
  97.             
  98.             if (this->InColorWindow())
  99.               {
  100.                 GetForeColor(&OldColor);
  101.                 RGBForeColor(&this->fontColor);
  102.               }
  103.             TextSize (this->fontSize);
  104.             TextFont (this->fontID);
  105.             TextFace (this->fontStyle);
  106.             TextBox (this->itemText+1, *this->itemText, 
  107.                      &this->bounds, this->fontJust);
  108.             
  109.             // restore the old font attributes
  110.             this->owningWindow->txFont = OldFont;
  111.             this->owningWindow->txSize = OldSize;
  112.             this->owningWindow->txFace = OldFace;
  113.             if (this->InColorWindow())
  114.               RGBForeColor(&OldColor);
  115.           }
  116.     }
  117.  
  118. /*-----------------------------------------------------------------*/
  119.  
  120.     void    CPPStaticText::MakeVisible (Boolean nowVisible)
  121.     /* hide or show the text item */
  122.     {
  123.           if (!nowVisible)
  124.             EraseRect (GetBounds());
  125.           InvalRect (GetBounds());
  126.       
  127.         CPPVisualObject::MakeVisible(nowVisible);
  128.     }
  129.  
  130. /*-----------------------------------------------------------------*/
  131.  
  132.     Rect    *CPPStaticText::GetBounds (void)
  133.     {
  134.         return &this->bounds;
  135.     }
  136.  
  137. /*-----------------------------------------------------------------*/
  138.     
  139.     void    CPPStaticText::MoveContent (short newH, short newV)
  140.     {
  141.         OffsetRect (&this->bounds, newH - this->bounds.left,
  142.                     newV - this->bounds.top);
  143.     }
  144.  
  145. /*-----------------------------------------------------------------*/
  146.  
  147.     void    CPPStaticText::ResizeContent (short newWidth, short newHeight)
  148.     {
  149.         this->bounds.right = this->bounds.left+newWidth;
  150.         this->bounds.bottom = this->bounds.top+newHeight;
  151.     }
  152.  
  153.  
  154. /*-----------------------------------------------------------------*/
  155.  
  156.     void    CPPStaticText::SetitsString (StringPtr newString, Boolean keepCopy)
  157.     {
  158.         GrafPtr    SavePort;
  159.         
  160.         GetPort(&SavePort);
  161.         SetPort(this->owningWindow);
  162.         
  163.         if (this->itemText)
  164.           DisposPtr((Ptr)this->itemText);
  165.         if (keepCopy)
  166.           this->itemText = String2String(newString);
  167.         else
  168.           this->itemText = newString;
  169.         
  170.         // force the string to redraw
  171.         EraseRect(GetBounds());
  172.         InvalRect(GetBounds());
  173.         SetPort(SavePort);
  174.     }
  175.  
  176. /*-----------------------------------------------------------------*/
  177.  
  178.     void    CPPStaticText::SetFont (short newFont)
  179.     {
  180.         GrafPtr    SavePort;
  181.         
  182.         GetPort(&SavePort);
  183.         SetPort(this->owningWindow);
  184.         if (this->fontSize != newFont)
  185.           {
  186.               this->fontSize = newFont;
  187.             // force the string to redraw
  188.             EraseRect (GetBounds());
  189.             InvalRect (GetBounds());
  190.           }
  191.         SetPort(SavePort);
  192.     }
  193.  
  194. /*-----------------------------------------------------------------*/
  195.  
  196.     void    CPPStaticText::SetFontStyle (short newFStyle)
  197.     {
  198.         GrafPtr    SavePort;
  199.         
  200.         GetPort(&SavePort);
  201.         SetPort(this->owningWindow);
  202.         if (this->fontStyle != newFStyle)
  203.           {
  204.               this->fontStyle = newFStyle;
  205.             // force the string to redraw
  206.             EraseRect (GetBounds());
  207.             InvalRect (GetBounds());
  208.           }
  209.         SetPort(SavePort);
  210.     }
  211.  
  212. /*-----------------------------------------------------------------*/
  213.  
  214.     void    CPPStaticText::SetFontSize (short newFSize)
  215.     {
  216.         GrafPtr    SavePort;
  217.         
  218.         GetPort(&SavePort);
  219.         SetPort(this->owningWindow);
  220.         if (this->fontSize != newFSize)
  221.           {
  222.               this->fontSize = newFSize;
  223.             // force the string to redraw
  224.             EraseRect (GetBounds());
  225.             InvalRect (GetBounds());
  226.           }
  227.         SetPort(SavePort);
  228.     }
  229.  
  230. /*-----------------------------------------------------------------*/
  231.  
  232.     void    CPPStaticText::SetJustification (short newJust)
  233.     {
  234.         GrafPtr    SavePort;
  235.         
  236.         GetPort(&SavePort);
  237.         SetPort(this->owningWindow);
  238.         if (this->fontJust != newJust)
  239.           {
  240.               this->fontJust = newJust;
  241.             // force the string to redraw
  242.             EraseRect (GetBounds());
  243.             InvalRect (GetBounds());
  244.           }
  245.         SetPort(SavePort);
  246.     }
  247.  
  248. /*-----------------------------------------------------------------*/
  249.  
  250.     void    CPPStaticText::SetColor (RGBColor *newColor)
  251.     {
  252.         GrafPtr    SavePort;
  253.         
  254.         GetPort(&SavePort);
  255.         SetPort(this->owningWindow);
  256.           this->fontColor = *newColor;
  257.         // force the string to redraw
  258.         InvalRect (GetBounds());
  259.         SetPort(SavePort);
  260.     }
  261.  
  262. /*-----------------------------------------------------------------*/
  263.  
  264.     StringPtr    CPPStaticText::GetitsString (void)
  265.     {
  266.         return this->itemText;
  267.     }
  268.  
  269. /*-----------------------------------------------------------------*/
  270.  
  271.     void    CPPStaticText::MakeStatText (Rect *itsBounds, short Font, 
  272.                                          short FSize, short FJust, 
  273.                                          short FStyle,
  274.                                          RGBColor *FColor)
  275.     {
  276.         this->bounds = *itsBounds;
  277.         this->fontID = Font;
  278.         this->fontSize = FSize;
  279.         this->fontJust = FJust;
  280.         this->fontStyle = FStyle;
  281.         this->fontColor = *FColor;
  282.     }